package com.itboye.ihomebank.me;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
/**
 * 修改性别
 *
 */
public class ActivitySex extends BaseOtherActivity implements OnClickListener,Observer{
	private ImageView close_icon;//返回图片
	private TextView add_shap_title_tv;//标题
	CheckBox sex_girl;
	CheckBox sex_boy;
	CheckBox sex_none;
	TextView sex_ok;
	
	private String selectedSex;
	
	private String user_sex;
	
	private UserPresenter userPresenter;
	
	private String uid;//id
	
	
	/**
	 * 获取性别  做出判断
	 */
	private void initSelect() {
		String sex = (String) SPUtils.get(ActivitySex.this, null,
				SPContants.SEX, "");
		
		if (sex.equals("0")) {
			sex_girl.setChecked(false);
			sex_boy.setChecked(false);
			sex_none.setChecked(true);
		}else if (sex.equals("1")) {
			sex_girl.setChecked(true);
			sex_boy.setChecked(false);
			sex_none.setChecked(false);
		}else {
			sex_girl.setChecked(false);
			sex_boy.setChecked(true);
			sex_none.setChecked(false);
		}
	}
	/**
	 * 当选中某个时值为多少
	 */
	private void checkSelect(){
		if(sex_girl.isChecked()==true){
			selectedSex="1";
		}else if(sex_boy.isChecked()==true){
			selectedSex="2";
		}else if(sex_none.isChecked()==true){
			selectedSex="0";
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
			
		case R.id.sex_ok:
			setSex();
			break;
		case R.id.sex_girl:
			sex_boy.setChecked(false);
			sex_none.setChecked(false);
			
			break;
		case R.id.sex_boy:
			sex_girl.setChecked(false);
			sex_none.setChecked(false);
			
			break;
		case R.id.sex_none:
			sex_boy.setChecked(false);
			sex_girl.setChecked(false);
			
			break;

		default:
			break;
		}
		
	}
	/**
	 * 修改性别
	 */
	private void setSex() {
		checkSelect();
		user_sex=String.valueOf(selectedSex);
		userPresenter=new UserPresenter(this);
		userPresenter.updateSex(uid, user_sex);
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity=handlerError(data);
		if(entity!=null){
			if(!entity.getCode().equals(0)){
				if (entity.getEventType() == UserPresenter.updateSex_success) {
					String bean = (String) entity.getData();
					if(bean!=null){
						ByAlert.alert("修改成功");
						SPUtils.put(ActivitySex.this, null, SPContants.SEX, user_sex);

						Intent intent = new Intent(ActivitySex.this,
								ActivityMyData.class);
						intent.putExtra("sex", user_sex);
						setResult(RESULT_OK, intent);
						ActivitySex.this.finish();
					}
					
				}else if(entity.getEventType() == UserPresenter.updateSex_fail){
					ByAlert.alert("修改失败");
				}
			}
		}
		
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_sex;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub

		add_shap_title_tv.setText("修改性别");
		
		initSelect();
		
		uid=(String)SPUtils.get(ActivitySex.this, null, SPContants.USER_ID, "");
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}
	
/*	private void setSex() {//修改性别
		checkSelect();
		user_sex=String.valueOf(selectedSex);
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_User_update")
				.param("uid",
						(String) SPUtils.get(ActivitySex.this, null, SPContants.USER_ID,
								""))
				.param("sex", user_sex)
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						DebugLog.v("result", response.toString());
						T.showShort(ActivitySex.this, "修改成功");
						SPUtils.put(ActivitySex.this, null, SPContants.SEX, user_sex);

						Intent intent = new Intent(ActivitySex.this,
								ActivityMyData.class);
						intent.putExtra("sex", user_sex);
						setResult(RESULT_OK, intent);
						ActivitySex.this.finish();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showShort(ActivitySex.this, "网络异常");
						} else {
							T.showShort(ActivitySex.this, "网络异常");
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}*/
	
}
